package com.my.study.util;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;

public class CountDownLatchUtil {

    public static String executorMainTask(int num, List<String> ids, executorMain executorMain) throws BrokenBarrierException, InterruptedException {
        List<String> strings = new ArrayList<>();
        List<List<String>> partition = Lists.partition(ids, num);
        CountDownLatch countDownLatch = new CountDownLatch(partition.size());

        for (int i = 0; i < partition.size(); i++) {
            int finalI = i;
            ThreadPoolUtil.submit(() -> {
                String result = "第" + finalI + "请求执行完成";
                System.out.println(result);
                strings.add(result);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("===============满足条件,执行自定义业务================" + countDownLatch.getCount());
        executorMain.executor(strings);
        return "success";
    }

    @FunctionalInterface
    public interface executorMain {
        List<String> executor(List<String> strings);
    }
}
